//
//  CZMainTabBar.swift
//  CZXSWWeibo007
//
//  Created by xushengwei on 15/12/16.
//  Copyright © 2015年 xushengwei. All rights reserved.
//

import UIKit

class CZMainTabBar: UITabBar {
    
    //属性，五个控件
    private let count = 5
    
    override func layoutSubviews() {
        //一定要调用父类的layoutSubviews
        super.layoutSubviews()
        //计算宽度
        let width = self.bounds.width / CGFloat(count)
        //最左边的frame
        let frame = CGRect(x: 0, y: 0, width: width, height: self.bounds.height)
        
        //遍历所有子控件
        var index = 0
        
        for view in subviews {
            // 设置 UITabBarButton 的frame
            // UITabBarButton 继承UIContorl
            // is: 判断类型
            if view is UIControl && !(view is UIButton) {
                view.frame = CGRectOffset(frame, width * CGFloat(index), 0)
                
                index += index == 1 ? 2 : 1
            }
        }
        
        //设置加好按钮的frame
        composeButton.frame = CGRectOffset(frame, width * 2.0, 0)
    }
    

   
    
    
    //懒加载 加好按钮
    lazy var composeButton: UIButton = {
        let button = UIButton()
        
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        //设置按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        self.addSubview(button)
        return button
        
    }()

}
